package com.lxk.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.pojo.Category;
import com.lxk.pojo.Product;
import com.lxk.pojo.Type;
import com.lxk.service.CategoryService;
import com.lxk.service.LoginService;
import com.lxk.service.ProductService;
import com.lxk.service.TypeService;
import com.lxk.util.ImgUrlUtil;
import com.lxk.util.TermUtil;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author JohnSoybean
 * @since 2020-07-14
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 一级分类列表
     * @return
     */
    @GetMapping("/type")
    public Result<List<Type>> typeList() {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.select("type_id", "type_name", "type_img");
        List<Type> list = typeService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 一级分类下的二级类目列表
     *
     * @return
     */
    @GetMapping("/category")
    public Result<List<Category>> categoryList(Long typeId) {
        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getTypeId, typeId));
        return Result.success(list);
    }

    /**
     * 展示所有商品
     *
     * @return
     */
    @GetMapping("/show")
    public Result<PageModel<Product>> showProductList(Integer pageNum, Integer pageSize, String sortProp, Integer sortOrder,
                                                      Long typeId, Long categoryId, String productName) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        //排序
        boolean isSort = TermUtil.validString(sortProp);
        wrapper.orderBy(TermUtil.validString(sortProp), TermUtil.isAsc(sortOrder), TermUtil.toCamel(sortProp));
        wrapper.orderByAsc(!isSort, "product_id");
        //排除字段
        wrapper.select(Product.class, i ->
                !( i.getProperty().equals("content")));
        //筛选
        wrapper.eq("upper", 1);
        wrapper.eq(TermUtil.validLong(typeId), "type_id", typeId);
        wrapper.eq(TermUtil.validLong(categoryId), "category_id", categoryId);
        wrapper.like(TermUtil.validString(productName), "product_name", productName);
        productService.page(page, wrapper);
        PageModel<Product> model = new PageModel<>(page);
        //处理图片：获取首图
        List<Product> productList = model.getList();
        for (Product product : productList) {
            String firstUrl = ImgUrlUtil.getFirstUrl(product.getProductImg());
            product.setProductImg(firstUrl);
        }
        //置入两级分类名
        List<Type> typeList = typeService.list();
        List<Category> categoryList = categoryService.list();
        Map<Long, String> typeMap = new HashMap<>();
        typeList.forEach(type -> {
            typeMap.put(type.getTypeId(), type.getTypeName());
        });
        Map<Long, String> categoryMap = new HashMap<>();
        categoryList.forEach(category -> {
            categoryMap.put(category.getCategoryId(), category.getCategoryName());
        });
        productList.forEach(product -> {
            product.setTypeName(typeMap.get(product.getTypeId()));
            product.setCategoryName(categoryMap.get(product.getCategoryId()));
        });
        model.setList(productList);
        return Result.success(model);
    }

    /**
     * 按id获取商品信息
     *
     * @return
     */
    @GetMapping("/get")
    public Result<Product> getProduct(Long productId) {
        Product product = productService.getById(productId);
        if (product == null || product.getUpper() == 0) {
            return Result.error(CodeMsg.GOOD_NOT_EXIST);
        }
        //处理图片
        List<String> imgList = ImgUrlUtil.splitUrl(product.getProductImg());
        product.setImgList(imgList);
        Type type = typeService.getById(product.getTypeId());
        if (type != null) {
            product.setTypeName(type.getTypeName());
        }
        Category category = categoryService.getById(product.getCategoryId());
        if (category != null) {
            product.setCategoryName(category.getCategoryName());
        }
        return Result.success(product);
    }

}

